package cn.ailanyin.home.module.cupboard.service.impl;

import cn.ailanyin.home.common.context.UserInfoContext;
import cn.ailanyin.home.common.exception.ServiceException;
import cn.ailanyin.home.common.utils.VOUtil;
import cn.ailanyin.home.module.cupboard.dto.CupboardDTO;
import cn.ailanyin.home.module.cupboard.entity.Cupboard;
import cn.ailanyin.home.module.cupboard.mapper.CupboardMapper;
import cn.ailanyin.home.module.cupboard.service.ICupboardSrvice;
import cn.ailanyin.home.module.cupboard.vo.CupboardVO;
import cn.ailanyin.home.module.things.entity.Things;
import cn.ailanyin.home.module.things.mapper.ThingsMapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小怪兽
 * @version 1.0
 * @date 2024/4/13 0013 19:59:16
 */
@Service
@RequiredArgsConstructor
public class CupboardServiceImpl implements ICupboardSrvice {

    /**
     * 橱柜映射器
     */
    private final CupboardMapper cupboardMapper;

    /**
     * 事物Mapper
     */
    private final ThingsMapper thingsMapper;


    @Override
    public List<CupboardVO> list(CupboardDTO dto) {
        LambdaQueryWrapper<Cupboard> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getLocation()), Cupboard::getLocation, dto.getLocation())
                .eq(Cupboard::getHomeId, UserInfoContext.getHomeId());
        List<Cupboard> list = cupboardMapper.selectList(wrapper);
        return VOUtil.getCupboardVOs(list);
    }

    @Override
    public void saveOrUpdate(CupboardDTO dto) {
        String location = dto.getLocation();
        if (StrUtil.isBlank(location)) {
            throw new ServiceException("储物柜位置不能为空");
        }
        isExists(dto);
        if (StrUtil.isBlank(dto.getId())) {
            // 新增
            Cupboard cupboard = new Cupboard();
            cupboard.setLocation(location);
            cupboard.setImgUrl(dto.getImgUrl());
            cupboard.setHomeId(UserInfoContext.getHomeId());
            cupboardMapper.insert(cupboard);
            return;
        }
        // 编辑
        LambdaUpdateWrapper<Cupboard> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(StrUtil.isNotBlank(location), Cupboard::getLocation, location)
                .set(StrUtil.isNotBlank(dto.getImgUrl()), Cupboard::getImgUrl, dto.getImgUrl())
                .eq(Cupboard::getId, dto.getId())
                .eq(Cupboard::getHomeId, UserInfoContext.getHomeId());
        cupboardMapper.update(wrapper);
    }

    @Override
    public void delete(String id) {
        // 删除柜子, 如果柜子里有物品, 则提示不让删除
        LambdaQueryWrapper<Things> thingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thingsLambdaQueryWrapper.eq(Things::getCupboardId, id);
        Long count = thingsMapper.selectCount(thingsLambdaQueryWrapper);
        if (count > 0){
            throw new ServiceException("柜子内存放了物品，无法删除");
        }
        LambdaQueryWrapper<Cupboard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cupboard::getHomeId, UserInfoContext.getHomeId())
                .eq(Cupboard::getId, id);
        cupboardMapper.delete(wrapper);
    }

    private void isExists(CupboardDTO dto) {
        String location = dto.getLocation();
        LambdaQueryWrapper<Cupboard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(location), Cupboard::getLocation, location)
                .eq(Cupboard::getHomeId, UserInfoContext.getHomeId())
            .ne(StrUtil.isNotBlank(dto.getId()), Cupboard::getId, dto.getId());
        boolean exists = cupboardMapper.exists(wrapper);
        if (exists) {
            throw new ServiceException("储物柜位置已存在！");
        }
    }
}
